﻿/************************************************************************
     Main File:

     File:        SJCFont.cpp

     Author:      
                  Steven Chenney, schenney@cs.wisc.edu
                  Yu-Chi Lai, yu-chi@cs.wisc.edu
 
     Comment:     Set up the font for printing character in opengl window

     Functions:
                  1. Constructor and destructor
                  2. PrintString: set up the message and print it on screen
	   	
     Compiler:    g++

     Platform:    Linux
*************************************************************************/

#include "SJCFont.h"

//***************************************************************************
//
// following are used to dispaly fonts.
//===========================================================================

const GLubyte SJCFont::m_uSpace[] = 
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const GLubyte SJCFont::m_uLetters [][13] = {
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18}, 
  {0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, 
  {0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e}, 
  {0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc}, 
  {0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff}, 
  {0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff}, 
  {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e}, 
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, 
  {0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 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 0xc3}, 
  {0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, 
  {0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, 
  {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, 
  {0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff},
  {0x3c, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7e, 0x3c}, 
  {0x7e, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x38},
  {0x7e, 0x7e, 0x60, 0x30, 0x30, 0x18, 0x18, 0x0c, 0x0c, 0x06, 0x66, 0x66, 0x3c},
  {0x3c, 0x66, 0x66, 0x06, 0x0c, 0x3c, 0x38, 0x3c, 0x0c, 0x06, 0x66, 0x66, 0x3c},
		{0x06, 0x06, 0x06, 0x06, 0x7f, 0x7f, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66},
  {0x7c, 0x7e, 0x06, 0x06, 0x06, 0x06, 0x7c, 0x7c, 0x60, 0x60, 0x60, 0x7e, 0x7e},
  {0x6c, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x7e, 0x7c, 0x60, 0x60, 0x66, 0x7e, 0x6c},
  {0x18, 0x18, 0x18, 0x18, 0x1c, 0x0c, 0x0c, 0x0c, 0x06, 0x66, 0x66, 0x7e, 0x6c},
  {0x6c, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x7e, 0x7e, 0x66, 0x66, 0x66, 0x7e, 0x6c},
  {0x6c, 0x7e, 0x66, 0x66, 0x06, 0x06, 0x3e, 0x7e, 0x66, 0x66, 0x66, 0x7e, 0x6c}
};


GLuint SJCFont::m_uFontOffset;
bool   SJCFont::m_bConstruct = false;
uint   SJCFont::m_uCount = 0;
	
//*************************************************************************
//
// The constructor
//=========================================================================
SJCFont::
SJCFont()
//=========================================================================
{
  if(!m_bConstruct)
    MakeRasterFont();
  m_bConstruct = true;
  m_uCount++;
}

//*************************************************************************
//
// The destructor
//=========================================================================
SJCFont::
~SJCFont()
//=========================================================================
{
  m_uCount--;
  if(!m_uCount)
    glDeleteLists(m_uFontOffset, 128);
  m_bConstruct = false;
}

//*************************************************************************
//
// Make the raster font
//=========================================================================
void SJCFont::
MakeRasterFont(void)
//=========================================================================
{
  GLuint i, j;
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  // Generate the font draw list for A to Z
  m_uFontOffset = glGenLists (128);
  for (i = 0, j = 'A'; i < 26; i++, j++) {
    glNewList(m_uFontOffset + j, GL_COMPILE);
    glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, m_uLetters[i]);
    glEndList();
  }
  // Generate the font draw list for 0 to 9
  for (i = 26, j = '0'; i < 36; i++, j++){
    glNewList(m_uFontOffset + j, GL_COMPILE);
    glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, m_uLetters[i]);
    glEndList();
  }

  // Create the space
  glNewList(m_uFontOffset + ' ', GL_COMPILE);
  glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, m_uSpace);
  glEndList();
}


//*************************************************************************
//
// Print out the string at desire location
//=========================================================================
void SJCFont::
PrintString(uint x, uint y, uint z, const char *s)
//=========================================================================
{
  glRasterPos3d(x, y, z);
  glPushAttrib (GL_LIST_BIT);
  glListBase(m_uFontOffset);
  glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s);
  glPopAttrib ();
}


